<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            到厂收货打印
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货单号</div>
                    <input type="text" class="aui-input" placeholder="请录入发货单号" id="consignment" onclick="getIdSelector();">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">蓝牙设备</div>
                    <select id="bluetooth">
                    </select>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="printAll()">一键打印条码</div>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="printSelect()">补打条码</div>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div>

        <div style="width:100%;margin-top:2%;"></div>
        <div style="background-color:white; width: 100%;margin-top:2%;white-space: nowrap;overflow-x: auto;-webkit-overflow-scrolling:touch;">
            <table class="table" style='width:1000px;' id="table1">
                <tbody style="display:block">
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                        <td algin='center' style="width:45px;"></td>
                        <td algin='center' style="width:45px;">序号</td>
                        <td algin='center' style="width:158px;">箱号</td>
                        <td algin='center' style="width:170px;">物料编码</td>
                        <td algin='center' style="width:158px;;">物料名称</td>
                        <td algin='center' style="width:110px;">批次号</td>
                        <td algin='center' style="width:80px;">数量</td>
                        <td algin='center' style="width:100px;">供应商批次</td>
                        <td algin='center' style="width:150px;">供应商编码</td>
                        <td algin='center' style="width:150px;">供应商名称</td>
                        <td style="display:none;">条码</td>
                        <td style="display:none;">规格型号</td>
                    </tr>
                </tbody>
            </table>
            <table class="table" style='width:1000px;' id="table">
                <tbody id="tbodyHtml" style="display: block;height: 200px;overflow-y: scroll;">
                </tbody>
            </table>
        </div>
    </div>

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script src="../script/webmsg/atmosphere.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script src="../script/echarts.min.js"></script>
<script type="text/javascript">
    var dataList;
    //初始化查询仓库下拉框
    apiready = function() {

        $("#consignment").focus();

        //获取蓝牙列表
        getBluetoothCombox();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        $("#" + el.id).select();
    }

    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.searchBluetooth({}, function(ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.connectBluetooth({
            address: address,
        }, function(ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }

    //根据发货单查询条码数据
    function selectData() {
        var consignment = $("#consignment").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getConsignmentHK";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                consignment: consignment,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    var list = eval(myobj.list);
                    var html = "";

                    //发货单明细数据存在
                    if (list != null && list != undefined && list.length > 0) {
                        dataList = list;
                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 12px;'>";
                            html += "<td style='width:40px;'><input name='sub' type='checkbox' /></td>";
                            html += "<td name='seqNo' style='width:50px;'>" + (i + 1) + "</td>";
                            html += "<td name='boxnum' style='width:130px;'>" + list[i].boxnum + "</td>";
                            html += "<td name='mrlCode' style='width:150px;'>" + list[i].mrlCode + "</td>";
                            html += "<td name='mrlName' style='width:150px;'>" + list[i].mrlName + "</td>";
                            html += "<td name='lotCode' style='width:100px;'>" + list[i].lotCode + "</td>";
                            html += "<td name='qty' style='width:80px;'>" + list[i].qty + "</td>";
                            if (list[i].ext01 == null || list[i].ext01 == "" || list[i].ext01 == undefined || list[i].ext01 == 'null') {
                                html += "<td name='supplierLotCode' style='width:120px;'></td>";
                            } else {
                                html += "<td name='supplierLotCode' style='width:120px;'>" + list[i].ext01 + "</td>";
                            }
                            html += "<td name='supplierCode' style='width:150px;'>" + list[i].supplierCode + "</td>";
                            html += "<td name='supplierName' style='width:150px;'>" + list[i].supplierName + "</td>";
                            html += "<td name='tm' style='display:none;'>" + list[i].tm + "</td>";
                            html += "<td name='specification' style='display:none;'>" + list[i].specification + "</td>";
                            html += "</tr>";
                        }

                    } else {
                        dataList = [];
                    }

                    $api.html($api.byId("tbodyHtml"), html);

                    //失败，存在没有订单数据
                } else if (myobj.code == "1") {

                    $("#consignment").select();
                    idSelector = "consignment";
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //一键打印打印
    function printAll() {
        var address = $("#bluetooth").val();

        //箱号非空校验
        if (dataList == null || dataList.length == 0) {
            api.toast({
                msg: '没有需要打印的数据！',
                duration: 3000
            });
            return;
        }

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var jsonHead = "[";
        var detailJson = "";
        var jsonTail = "]";

        var boxnum = "";
        var supplierName = "";
        var mrlCode = "";
        var mrlName = "";
        var lotCode = "";
        var supplierLotCode = "";
        var qty = "";
        var tm = "";
        var specification = "";
        var printTime = "";

        var date = new Date();
        var month = date.getMonth() < 10 ? "0" + date.getMonth() : date.getMonth();
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        printTime = date.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

        for (var i = 0; i < dataList.length; i++) {
            boxnum = dataList[i].boxnum;
            supplierName = dataList[i].supplierName;
            mrlCode = dataList[i].mrlCode;
            mrlName = dataList[i].mrlName;
            lotCode = dataList[i].lotCode;
            supplierLotCode = dataList[i].ext01;
            qty = dataList[i].qty;
            tm = dataList[i].tm;
            specification = dataList[i].specification;

            detailJson = detailJson + "{\"mrlCode\":\"" + mrlCode + "\",\"mrlName\":\"" + mrlName +
                "\",\"supplierName\":\"" + supplierName + "\",\"boxnum\":\"" + boxnum +
                "\",\"lotCode\":\"" + lotCode + "\",\"supplierLotCode\":\"" + supplierLotCode +
                "\",\"qty\":\"" + qty + "\",\"tm\":\"" + tm + "\",\"specification\":\"" + specification +
                "\",\"printTime\":\"" + printTime + "\"},";
        }


        if (detailJson != null && detailJson != "" && detailJson != undefined) {
            detailJson = detailJson.substr(0, detailJson.length - 1);
        }
        detailJson = jsonHead + detailJson + jsonTail;

        var json = eval(detailJson);

        //调用打印
        printPDA(json, address);

    }

    //补打条码
    function printSelect() {

        var address = $("#bluetooth").val();

        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function() {
            count++;
        });

        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择要补打的箱号！',
                duration: 3000
            });
            return;
        }

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var jsonHead = "[";
        var detailJson = "";
        var jsonTail = "]";

        var boxnum = "";
        var supplierName = "";
        var mrlCode = "";
        var mrlName = "";
        var lotCode = "";
        var supplierLotCode = "";
        var qty = "";
        var tm = "";
        var specification = "";
        var printTime = "";

        var date = new Date();
        var month = date.getMonth() < 10 ? "0" + date.getMonth() : date.getMonth();
        var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        printTime = date.getFullYear() + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

        check.each(function() { //遍历
            var row = $(this).parent("td").parent("tr");
            boxnum = row.find("[name='boxnum']").html();
            supplierName = row.find("[name='supplierName']").html();
            mrlCode = row.find("[name='mrlCode']").html();
            mrlName = row.find("[name='mrlName']").html();
            lotCode = row.find("[name='lotCode']").html();
            supplierLotCode = row.find("[name='supplierLotCode']").html();
            qty = row.find("[name='qty']").html();
            tm = row.find("[name='tm']").html();
            specification = row.find("[name='specification']").html();

            detailJson = detailJson + "{\"mrlCode\":\"" + mrlCode + "\",\"mrlName\":\"" + mrlName +
                "\",\"supplierName\":\"" + supplierName + "\",\"boxnum\":\"" + boxnum +
                "\",\"lotCode\":\"" + lotCode + "\",\"supplierLotCode\":\"" + supplierLotCode +
                "\",\"qty\":\"" + qty + "\",\"tm\":\"" + tm + "\",\"specification\":\"" + specification +
                "\",\"printTime\":\"" + printTime + "\"},";
        });

        if (detailJson != null && detailJson != "" && detailJson != undefined) {
            detailJson = detailJson.substring(0, detailJson.length - 1);
        }
        detailJson = jsonHead + detailJson + jsonTail;

        var json = eval(detailJson);

        //调用打印
        printPDA(json, address);
    }

    //打印
    function printPDA(json, address) {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.printerPlatformCt({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function(ret, err) {
            if (ret) {

                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //条码监听回车事件
    $("#consignment").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            $("#consignment").val(tm);
            //查询表格数据
            selectData();
        }
    });

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            //发货单号
            $("#consignment").val(ret);

            //查询表格数据
            selectData();

        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
